package cn.haocxy.jfm.util;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javafx.application.Platform;

public final class Executor {

	private Executor() {
	}

	private static final java.util.concurrent.Executor executor = Executors
			.newCachedThreadPool();

	// 定时器
	private static final ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
	
	/**
	 * 在不同于UI线程的线程中执行
	 */
	public static void exe(Runnable r) {
		executor.execute(r);
	}
	
	/**
	 * 在UI线程中执行
	 */
	public static void exeui(Runnable r) {
		Platform.runLater(r);
	}
	
	/**
	 * 延迟ms毫秒后在UI线程执行r
	 */
	public static void delayui(long ms, Runnable r) {
		if (ms >= 0 && r != null) {
			timer.schedule(()->exeui(r), ms, TimeUnit.MILLISECONDS);
		}
	}
}
